package cn.aresoft.framework.plugins.pagination.jdbc;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.stereotype.Component;


/**
 *  <p>Title: PaginateServiceImpl</p>
 *  <p>Description: PaginateService</p>
 *  <p>Copyright: Copyright (c) 2010-6-18</p>
 *  <p>Company: 蓝色天空工作室 </p>
 *
 * @author ShiGuanghui
 */
@Component
public class PaginateServiceImpl implements PaginateService {
	

//	@Autowired
//	@Qualifier("jdbcDaoSupportImpl")
//	private JdbcDaoSupportService jdbcDaoSupportService;
//	
//	/**
//	 *  获取总条数 hibernate
//	 */
//	public int getTotalCount(String sql, Map paras) throws Exception {
//		sql = "select count(*) " + removeSelect(removeOrders(sql));
//		return jdbcDaoSupportService.queryForIntJ(sql, paras);
//	}
//
//	public List getList(String hql, Map paras, PageBeanJ pageBean) throws Exception {
//		return jdbcDaoSupportService.getSubListByParamsJ(hql, paras, (pageBean.getPage() - 1) * pageBean.getPageSize(), pageBean.getPageSize());
//	}
	
	
	private String removeSelect(String hql) {
		int beginPos = hql.toLowerCase().indexOf("from");
		return hql.substring(beginPos);
	}

	private String removeOrders(String hql) {
		
		Pattern p = Pattern.compile("order\\s*by[\\w|\\W|\\s|\\S]*", Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(hql);
		
		StringBuffer sb = new StringBuffer();
		while (m.find()) {
			m.appendReplacement(sb, "");
		}
		m.appendTail(sb);
		return sb.toString();
	}

	public List getList(String sql, Map paras, PageBeanJ pageBeanJ)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public int getTotalCount(String sql, Map paras) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

}
